package com.gdit.zax.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

public class O {
	public static void printSuccess(HttpServletResponse response,Object data) {
		if (data==null) {
			print(response, 1000, null, "database server error");
		} else {
			print(response, 0, data, "success");
		}
	}
	
	public static void printSuccess(HttpServletResponse response,String msg) {
		print(response, 0, null, msg);
	}
	
	public static void printDBerror(HttpServletResponse response) {
		print(response, 1000,null, "database server error!");
	}
	
	public static void printNoLogin(HttpServletResponse response) {
		print(response, 100 ,null, "请重新登录!");
	}
	
	public static void printFail(HttpServletResponse response,int errorcode,String message) {
		print(response, errorcode, null, message);
	}
	
	public static void print(HttpServletResponse response,int errorcode,Object data,String message) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("errorcode", errorcode);
		map.put("data", data);
		map.put("message", message);
		print(response, map);
	}
	
	
	public static void print(HttpServletResponse response,Map<String,Object> map) {
		LogUtil.print("=============================================" + new Date().toLocaleString());
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out;
		try {
			out = response.getWriter();
			
			JSONObject json = new JSONObject();
			for (String key : map.keySet()) {
				LogUtil.print(key + ":" + map.get(key));
				json.put(key, map.get(key));
			}
			out.print(json);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	
	
//	//从特定的list获取只需要的信息
//	public static List deailList(List list,String... need) {
//		List nelist = new ArrayList<Object>();
//		for (Object obj:list) {
//			Map<String, String> map = new HashMap<String, String>();
//			for (String key:need) {
//				Method m = obj.getClass().getMethods()[0];
//				m.
//				map.put(key, value)				
//			}
//			
//			
//		}
//		
//		return null;		
//	}
	
}
